import gri.gridp.GridpFactory;
import gri.gui.files.views.FileViewManager;
import gri.gui.files.views.ViewManagerSerializer;
import gri.tasks.gui.icons.TestShell;
import gri.tasks.managers.JobManagerSet;
import gri.tasks.managers.TaskManager;
import gri.tasks.managers.BasicJobManager;
import java.io.File;
import java.io.PrintStream;
/**
* Command line program for running the GRIDP GUI. This will
* load a set of JobManagers and start an application that allows
* us to run jobs.
*
* @author rogersda
*/
public class gridp_gui {
File managerConfigFile;
File viewConfigFile;
File moduleFolder;
boolean windowsMode = true;
// ----------------------------------------------------- Execution
public static void main(String [] args) {
dan.swing.style.AppStyles.loadSystemLookAndFeel();
gri.tasks.gui.DefaultStyles.load();
gri.tasks.gui.icons.DefaultStyles.load();
gri.tasks.gui.workflows.DefaultStyles.load();
gridp_gui main = new gridp_gui();
try {
boolean valid = main.loadParams(args);
if (valid)
main.execute();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void printHelp(PrintStream out) {
out.println();
out.println("GRIDP Graphical Client");
out.println();
out.println("Syntax:");
out.println();
out.println(" gridp_gui [parameters]");
out.println();
out.println("Parameters:");
out.println();
out.println(" --managers [file]");
out.println(" Config file (XML) defining job managers");
out.println(" default: managers.xml");
out.println();
out.println(" --modules [folder]");
out.println(" Folder containing GRIDP XML modules to load");
out.println(" These will be loaded into a separate job manager");
out.println(" default: modules");
out.println();
out.println(" --views [file]");
out.println(" Config file (XML) defining file viewers to use");
out.println(" These are often system-specific");
out.println(" default: views.xml");
out.println();
out.println(" --help Print help information");
out.println();
out.println("System Parameters:");
out.println(" --unix Run GRIDP programs in Unix mode");
out.println(" --windows Run GRIDP programs in Windows mode");
out.println();
}
/**
* Configures properties using the command line arguments. True is
* returned if we are ready to run the program.
*/
public boolean loadParams(String [] args) {
//parse:
for (int i=0; i<args.length; i++) {
String arg = args[i];
if (arg.equals("--managers"))
managerConfigFile = new File(args[++i]);
else if (arg.equals("--views"))
viewConfigFile = new File(args[++i]);
else if (arg.equals("--modules"))
moduleFolder = new File(args[++i]);
else if (arg.equals("--windows"))
windowsMode = true;
else if (arg.equals("--unix"))
windowsMode = false;
else if (arg.equals("--help")) {
printHelp(System.out);
return false;
}
else {
System.err.println("Unknown or unexpected parameter: " + arg);
printHelp(System.err);
return false;
}
}
//validate:
if (managerConfigFile != null && !isValid(managerConfigFile, true))
return false;
if (viewConfigFile != null && !isValid(viewConfigFile, true))
return false;
if (moduleFolder != null && !isValid(moduleFolder, false))
return false;
//supply defaults:
if (managerConfigFile == null) {
File defaultManagers = new File("managers.xml");
if (defaultManagers.exists() && defaultManagers.isFile())
managerConfigFile = defaultManagers;
}
if (viewConfigFile == null) {
File defaultViews = new File("views.xml");
if (defaultViews.exists() && defaultViews.isFile())
viewConfigFile = defaultViews;
}
if (moduleFolder == null) {
File defaultFolder = new File("modules");
if (defaultFolder.exists() && defaultFolder.isDirectory())
moduleFolder = defaultFolder;
}
//make sure we have everything we need:
if (managerConfigFile == null && moduleFolder == null) {
System.err.println("Cannot start program without a manager config file or a GRIDP module folder");
printHelp(System.err);
return false;
}
return true;
}
/**
* Validates that a file exists and is of the correct type (file/folder)
*/
protected boolean isValid(File file, boolean isFile) {
if (!file.exists()) {
System.err.println("File not found: " + file.getPath());
return false;
}
if (isFile && !file.isFile()) {
System.err.println("Directory found where file was expected: " + file.getPath());
return false;
}
if (!isFile && file.isFile()) {
System.err.println("File found where directory was expected: " + file.getPath());
return false;
}
return true;
}
/**
* Runs the program:
*/
public void execute() throws Exception {
JobManagerSet jobManagers = loadJobManagers();
//FileViewManager viewManager = loadViewManager();
TestShell shell = new TestShell();
//TODO: configure (?)
shell.runTest(jobManagers);
}
/**
* Loads the FileViewManager
* TODO: supply default
*/
protected FileViewManager loadViewManager() throws Exception {
return ViewManagerSerializer.read(viewConfigFile);
}
/**
* Loads the JobManagerSet used in the program
*/
protected JobManagerSet loadJobManagers() throws Exception {
//load from config file:
JobManagerSet jobManagers = new JobManagerSet();
if (managerConfigFile != null)
jobManagers.addAll(Loader.load(managerConfigFile));
//load GRIDP JobManager from module folder:
if (moduleFolder != null) {
int mode = windowsMode ?
GridpFactory.WINDOWS : GridpFactory.UNIX;
GridpFactory factory = new GridpFactory(moduleFolder, mode);
TaskManager taskManager = factory.getTaskManager();
BasicJobManager jobManager = new BasicJobManager(taskManager);
//auto-generate ID:
String id = "GRIDP Modules";
if (jobManagers.hasJobManager(id)) {
String idBase = id;
int index = 1;
while (jobManagers.hasJobManager(id)) {
index++;
id = idBase + " (" + index + ")";
}
}
jobManagers.addJobManager(id, jobManager);
}
return jobManagers;
}
}